package com.bristua.mall.wx.biz;
import com.bristua.mall.common.utils.CacheUtil;
import com.bristua.mall.common.utils.DateUtil;
import com.bristua.mall.common.utils.JsonUtil;
import com.bristua.mall.common.utils.SequenceUtil;
import com.bristua.mall.user.dao.TUserAccountMapper;
import com.bristua.mall.user.dao.TUserMapper;
import com.bristua.mall.user.model.TUser;
import com.bristua.mall.user.model.TUserAccount;
import com.bristua.mall.user.model.condition.TUserExample;
import com.bristua.mall.wx.model.WXConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.client.RestTemplate;
import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2019/4/17.
 */
@Service
@Transactional
public class AsyncAddWxUserBiz {

    @Resource
    private TUserMapper tUserMapper;

    @Autowired
    private WXConfig wxConfig;

    @Autowired
    private AppGetTokenBiz appGetTokenBiz;

    @Autowired
    private RestTemplate restTemplate;

    @Autowired
    private CacheUtil cacheUtil;

    @Resource
    private TUserAccountMapper tUserAccountMapper;

    public String addWxUser(String openId,String token){
        TUserExample tUserExample = new TUserExample();
        tUserExample.createCriteria().andOpenIdEqualTo(openId);
        List<TUser> tUsers = tUserMapper.selectByExample(tUserExample);
        if(tUsers==null || tUsers.size()==0){
            Map userInfo = getUserInfo(token);
            TUser tUser = new TUser();
            tUser.setStatus("1");
            tUser.setUserId(SequenceUtil.nextId());
            tUser.setCreateTime(DateUtil.getDateTime3());
            tUser.setCityName(String.valueOf(userInfo.get("city")));
            tUser.setProvinceName(String.valueOf(userInfo.get("province")));
            tUser.setCountryName(String.valueOf(userInfo.get("country")));
            tUser.setOpenId(String.valueOf(userInfo.get("openid")));
            tUser.setUserSex(String.valueOf(Double.valueOf(String.valueOf(userInfo.get("sex"))).intValue()));
            tUser.setNickName(String.valueOf(userInfo.get("nickname")));
            tUser.setUserImgUrl(String.valueOf(userInfo.get("headimgurl")));
            tUser.setUserName(String.valueOf(userInfo.get("nickname")));
            tUser.setUnionid(String.valueOf(userInfo.get("unionid")));
            tUser.setUserCode(createUserCode());
            tUser.setUserLevel("0");
            tUserMapper.insert(tUser);

            TUserAccount tUserAccount = new TUserAccount();
            tUserAccount.setUserAccountStatus("1");
            tUserAccount.setUserAccountId(SequenceUtil.nextId());
            tUserAccount.setUserId(tUser.getUserId());
            tUserAccount.setUserIntegral("0");
            tUserAccountMapper.insert(tUserAccount);

            return cacheUtil.put(openId+"_userinfo",tUser);
        }
        return cacheUtil.put(openId+"_userinfo",tUsers.get(0));
    }


    private String createUserCode() {
        String s = SequenceUtil.generateShortUuid();
        TUserExample tUserExample = new TUserExample();
        tUserExample.createCriteria().andUserCodeEqualTo(s);
        List<TUser> tUsers = tUserMapper.selectByExample(tUserExample);
        if (tUsers == null || tUsers.size() == 0) {
            return s;
        }
        createUserCode();
        return null;

    }


    private Map getUserInfo(String openId){
        String token = appGetTokenBiz.appGetToken(null, openId);
        String appGetUserInfoUrl = wxConfig.getAppGetUserInfoUrl();
        appGetUserInfoUrl = String.format(appGetUserInfoUrl,token,openId);
        ResponseEntity<String> forEntity = restTemplate.getForEntity(appGetUserInfoUrl, String.class);
        String body = forEntity.getBody();
        return JsonUtil.json2Map(body);
    }

}
